package com.fuhe.chen.vendingmachine.controller.supplier;

import com.alipay.api.response.AlipayTradeRefundResponse;
import com.fuhe.chen.vendingmachine.alipay.Alipay;
import com.fuhe.chen.vendingmachine.constant.MessageConstant;
import com.fuhe.chen.vendingmachine.constant.WebConst;
import com.fuhe.chen.vendingmachine.dto.ApiResponse;
import com.fuhe.chen.vendingmachine.dto.ApiResponseForList;
import com.fuhe.chen.vendingmachine.dto.cond.BookOrderCond;
import com.fuhe.chen.vendingmachine.pojo.*;
import com.fuhe.chen.vendingmachine.service.IBookOrderService;
import com.fuhe.chen.vendingmachine.service.IUserService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/supplier/bookOrderAdmin")
@ResponseBody
@Api(tags = "供货商订单管理")
public class SupplierOrderAdminController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SupplierOrderAdminController.class);

    @Autowired
    IBookOrderService bookOrderService;

    @Autowired
    IUserService userService;

    @ApiOperation(value = "查询用户订单列表", notes = "参数:用户id,订单状态,页数")
    @PostMapping("/getBookOrderList")
    public ApiResponseForList<BookOrder> getBookOrderList(@RequestParam(name = "machineId", required = true) Integer machineId,
                                                          @RequestParam(name = "status", required = false) Integer status,
                                                          @RequestParam(name = "orderId", required = false) String orderId,
                                                          @RequestParam(name = "user_id", required = false) String user_id,
                                                          @RequestParam(name = "payMethod", required = false) Integer payMethod,
                                                          @RequestParam(name = "create_date_start", required = false) Long create_date_start,
                                                          @RequestParam(name = "create_date_end", required = false) Long create_date_end,
                                                          @RequestParam(name = "pay_date_start", required = false) Long pay_date_start,
                                                          @RequestParam(name = "pay_date_end", required = false) Long pay_date_end,
                                                          @RequestParam("pageNum") Integer pageNum,
                                                          HttpServletRequest request
    ) {
        ApiResponseForList<BookOrder> response = new ApiResponseForList<>();

        //检查当前登录者是否有权限查询此机器信息
        User userInfo = (User) request.getSession().getAttribute(WebConst.LOGIN_SESSION_KEY);
        RelationshipSupplierMachine relationshipByCondition = userService.getRelationshipByMachineId(machineId);
        if (relationshipByCondition == null || !userInfo.getId().equals(relationshipByCondition.getSupplier_id())) {
            response.setSuccess(false);
            response.setCode(MessageConstant.Error.NO_QUERY_PERMISSION_CODE);
            response.setMessage(MessageConstant.Error.NO_QUERY_PERMISSION_MESSAGE);

            return response;
        }

        BookOrderCond cond = new BookOrderCond();
        cond.setUser_id(user_id);
        cond.setMachineId(machineId);
        cond.setOrderId(orderId);
        cond.setPayMethod(payMethod);
        cond.setStatus(status);
        cond.setCreate_date_start(create_date_start);
        cond.setCreate_date_end(create_date_end);
        cond.setPay_date_start(pay_date_start);
        cond.setPay_date_end(pay_date_end);

        PageInfo<BookOrder> bookOrderPageInfo = bookOrderService.queryOrderByCondition(cond, pageNum, 10);
        response.setData(bookOrderPageInfo.getList());
        response.setSuccess(true);
        response.setCode(MessageConstant.Success.SUCCESS_CODE);
        response.setMessage(MessageConstant.Success.QUERY_SUCCESS_MESSAGE);

        return response;
    }

    @ApiOperation(value = "查询当前登陆者所拥有的机器列表")
    @PostMapping("/getMachineListForSupplier")
    public ApiResponseForList<Machine> getMachineListForSupplier(HttpServletRequest request) {

        User userInfo = (User) request.getSession().getAttribute(WebConst.LOGIN_SESSION_KEY);
        List<Machine> machines = userService.getMachineListBySupplierId(userInfo.getId());

        ApiResponseForList<Machine> response = new ApiResponseForList<>();
        response.setSuccess(true);
        response.setCode(MessageConstant.Success.SUCCESS_CODE);
        response.setMessage(MessageConstant.Success.QUERY_SUCCESS_MESSAGE);
        response.setData(machines);
        return response;
    }


    @ApiOperation(value = "查询订单详情", notes = "参数:订单id")
    @PostMapping("/getOrderDetail")
    public ApiResponse<BookOrder> getOrderDetail(@RequestParam("order_id") String order_id) {

        BookOrder bookOrder = bookOrderService.queryOrderByOrderIdForAdmin(order_id);

        ApiResponse<BookOrder> response = new ApiResponse<>();
        response.setSuccess(true);
        response.setCode(MessageConstant.Success.SUCCESS_CODE);
        response.setMessage(MessageConstant.Success.QUERY_SUCCESS_MESSAGE);
        response.setData(bookOrder);
        return response;
    }

    @ApiOperation(value = "门店管理锁定客户预约单")
    @PostMapping({"/determineAddBookOrder"})
    @ResponseBody
    public ApiResponseForList<String> determineAddBookOrder(
            @RequestParam(value = "machineID", required = true) Integer machineID,
            @RequestParam(value = "bookOrderID", required = true) String bookOrderID) {


        String pickCode = bookOrderService.determineAddBookOrder(machineID, bookOrderID);

        ApiResponseForList<String> response = new ApiResponseForList<>();
        response.setSuccess(true);
        response.setCode(MessageConstant.Success.SUCCESS_CODE);
        response.setMessage(pickCode);

        return response;
    }

    @ApiOperation(value = "门店确认顾客取货")
    @PostMapping({"/determinePickUpOrder"})
    @ResponseBody
    public ApiResponse<String> determinePickUpOrder(@RequestParam("orderId") String orderId) {
        ApiResponse<String> response = new ApiResponse<>();

        BookOrder bookOrder = bookOrderService.queryOrderByOrderId(orderId);
        if (bookOrder != null && bookOrder.getOrder_status() == 2) {
            bookOrderService.determinePickUpOrder(bookOrder);
            response.setSuccess(true);
            response.setCode(10000);
            response.setMessage("取货成功");
        } else {
            response.setSuccess(false);
            response.setCode(15555);
            response.setMessage("取货失败,未找到订单");
        }
        return response;
    }

    @ApiOperation(value = "门店通过取货码和门店id获取订单信息")
    @PostMapping({"/queryOrderByPickCode"})
    @ResponseBody
    public ApiResponse<BookOrder> queryOrderByPickCode(@RequestParam("machineId") Integer machineId,
                                                       @RequestParam("pickCode") String pickCode) {
        ApiResponse<BookOrder> response = new ApiResponse<>();

        BookOrder bookOrder = bookOrderService.queryOrderByPickCode(machineId, pickCode);
        if (bookOrder != null && bookOrder.getOrder_status() == 2) {
            response.setSuccess(true);
            response.setCode(MessageConstant.Success.SUCCESS_CODE);
            response.setMessage(MessageConstant.Success.QUERY_SUCCESS_MESSAGE);
            response.setData(bookOrder);
        } else {
            response.setSuccess(false);
            response.setCode(MessageConstant.BookOrder.BOOK_ORDER_IS_NULL_CODE);
            response.setMessage(MessageConstant.BookOrder.BOOK_ORDER_IS_NULL);
        }
        return response;
    }


}
